<template>
	<el-form ref="loginForm" :model="form" :rules="rules" label-width="0" size="large" @keyup.enter="login">
		<el-form-item prop="username">
			<el-input v-model="form.username" prefix-icon="user" clearable :placeholder="$t('login.userPlaceholder')"></el-input>
		</el-form-item>
		<el-form-item prop="password">
			<el-input v-model="form.password" prefix-icon="lock" clearable show-password :placeholder="$t('login.PWPlaceholder')"></el-input>
		</el-form-item>
		<el-form-item prop="code">
			<div class="login-msg-yzm">
				<el-input v-model="form.code" prefix-icon="lock" clearable placeholder="请输入验证码，点击右边小图标获取"></el-input>
				<el-popover :width="200">
					<template #reference>
						<el-icon style="font-size: 32px; margin: 4px; color: var(--el-color-primary); cursor: pointer"><Pointer /></el-icon>
					</template>
					<el-image src="/qrcode.png" />
				</el-popover>
			</div>
		</el-form-item>
		<el-form-item style="margin-bottom: 10px">
			<el-col :span="12">
				<el-checkbox v-model="form.autologin" :label="$t('login.rememberMe')"></el-checkbox>
			</el-col>
			<el-col :span="12" class="login-forgot">
				<router-link to="/reset_password">{{ $t('login.forgetPassword') }}？</router-link>
			</el-col>
		</el-form-item>
		<el-form-item>
			<el-button type="primary" style="width: 100%" :loading="loading" round @click="login">{{ $t('login.signIn') }}</el-button>
		</el-form-item>
		<div v-if="false" class="login-reg">
			{{ $t('login.noAccount') }} <router-link to="/user_register">{{ $t('login.createAccount') }}</router-link>
		</div>
	</el-form>
</template>

<script>
import useStore from '@/store'
const { user } = useStore()

export default {
	data() {
		return {
			form: {
				username: 'admin',
				password: 'Aa!123',
				code: 'asd123',
				autologin: false
			},
			rules: {
				username: [{ required: true, message: this.$t('login.userError'), trigger: 'blur' }],
				password: [{ required: true, message: this.$t('login.PWError'), trigger: 'blur' }],
				code: [{ required: true, message: this.$t('login.codeError'), trigger: 'blur' }]
			},
			loading: false
		}
	},
	watch: {
		'$route.query': {
			handler(query) {
				user.$patch({
					loginQuery: query
				})
			},
			immediate: true
		}
	},
	mounted() {},
	methods: {
		async login() {
			let validate = await this.$refs.loginForm.validate().catch(() => {})
			if (!validate) {
				return false
			}
			this.loading = true
			user.login(this.form).finally(() => {
				this.loading = false
			})
		}
	}
}
</script>

<style></style>
